package com.util.library.sound;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;

/**
 * Class SoundPlay is used to play the sound
 * 
 * 
 * 
 * @author Tan Dinh (dinhngoctan.cntt@gmail.com)
 */
public final class SoundPlay
{
	private static MediaPlayer _player = new MediaPlayer();

	public static final int play(File audioFile)
	{
		try
		{
			return play(new FileInputStream(audioFile).getFD());
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
			return -1;
		} catch (IOException e)
		{
			e.printStackTrace();
			return -1;
		}

	}

	public static final void play(File audioFile, int repeat)
	{
		FileInputStream fileInputStream;
		int duration = 0;
		try
		{
			fileInputStream = new FileInputStream(audioFile);
			duration = play(fileInputStream.getFD());
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		if (duration > 0)
		{
			if (repeat > 0)
			{
				repeat(repeat);
			}
			else if (repeat < 0)
			{
				_player.setLooping(true);
			}
		}
	}

	public static final void play(FileInputStream fileInputStream, int repeat)
	{
		int duration = 0;
		try
		{
			duration = play(fileInputStream.getFD());
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		if (duration > 0)
		{
			if (repeat > 0)
			{
				repeat(repeat);
			}
			else if (repeat < 0)
			{
				_player.setLooping(true);
			}
		}
	}

	public static final void play(FileDescriptor fileDescriptor, int repeat)
	{
		int duration = 0;
		duration = play(fileDescriptor);

		if (duration > 0)
		{
			if (repeat > 0)
			{
				repeat(repeat);
			}
			else if (repeat < 0)
			{
				_player.setLooping(true);
			}
		}
	}

	public static final void play(Context context, int rawResourceId, int repeat)
	{
		int duration = 0;
		duration = play(context, rawResourceId);

		if (duration > 0)
		{
			if (repeat > 0)
			{
				repeat(repeat);
			}
			else if (repeat < 0)
			{
				_player.setLooping(true);
			}
		}
	}

	private static void repeat(int repeat)
	{
	}

	public static final int play(FileDescriptor fileDescriptor)
	{
		if (_player == null)
		{
			_player = new MediaPlayer();
		}
		
		try
		{
			_player.setDataSource(fileDescriptor);
		} catch (IllegalArgumentException e)
		{
			e.printStackTrace();
		} catch (IllegalStateException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		play();
		return _player.getDuration();
	}

	public static final int play(Context context, int rawResourceId)
	{
		_player = MediaPlayer.create(context, rawResourceId);		
		_player.start();
		return _player.getDuration();
	}

	public static boolean isPlaying()
	{
		if (_player != null && _player.isPlaying())
			return true;
		return false;
	}

	private static void play()
	{
		if (_player == null)
		{
			_player = new MediaPlayer();
		}

		try
		{
			_player.prepare();
		} catch (IllegalStateException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		_player.start();

	}

	public static void stop()
	{
		if (_player != null)
		{
			_player.stop();
			_player.release();
			_player = null;
		}
	}
}
